package com.fairytask.ui
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.MoneyDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.engine.define.ViewDefine;
	import com.fairytask.engine.resource.StaticIni;
	import com.jzonl.event.ChatEvent;
	import com.jzonl.managers.TipsManage;
	import com.jzonl.net.event.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToClientEvent;
	import com.jzonl.net.messages.CustomToServerFlag;
	import com.jzonl.net.mgr.NetMgr;
	import com.jzonl.tools.MoneyTools;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.CardItem;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 区域通关结果 
	 * @author xujh
	 * 
	 */
	public class AreaResultUI extends FrameUI
	{
		private static var _self	:AreaResultUI;
		
		//遮罩,防点击其他
		public var maskMc		:MovieClip;
		public var passMc		:Clip;
		//评星容器
		public var starSp		:Sprite = new Sprite();
		public var closeBtn	:Button;	//关闭
		
		public var cardBG		:MovieClip;
		public var cardVG		:ViewGroup;	//卡牌容器
		public var infoLab		:Label;
		public var moneySp		:Sprite = new Sprite();
		public var needLab		:Clip;		//屏蔽费用
		public var silverLab	:Label;		//银两
		public var goldLab		:Label;		//仙玉
		public var hasSilver	:Label;
		public var hasGold		:Label;
		
		private var _curScene	:int;		//当前场景id
		private var _cardNum	:int = 8;	//卡牌总数
		private var _isRemove	:Boolean = true;	//是否屏蔽状态
		private var _hasClick	:Boolean = false;	//抽取卡牌
		private var _clickPos	:int;		//抽取卡片的位置
		//定时器
//		private const TIMESET	:int = 30;	//计时30秒
//		private var _timer		:Timer = new Timer(TIMESET*1000,1);	//30秒关闭
		//颜色处理数据
		private var _needGold		:int;
		private var _needSilver	:int;
		private var _hasGold		:int;
		private var _hasSilver		:int;
		
		public static function getInstance():AreaResultUI
		{
			if(_self == null)
			{
				_self	=	new AreaResultUI();				
			}
			return _self;
		}
		
		public function AreaResultUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEvent();
			initBind();
		}
		
		/**
		 * 初始化 
		 * 
		 */
		private function init():void
		{
			windowBG.addChild(starSp);
			windowBG.addChild(moneySp);
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			cardVG.addItemsEvent(MouseEvent.CLICK,onClick);
			cardVG.addItemsEvent(MouseEvent.MOUSE_OVER,onOver);
			cardVG.addItemsEvent(MouseEvent.MOUSE_OUT,onOut);
			//关闭
			closeBtn.addEventListener(MouseEvent.CLICK,onClose);
			//屏蔽掉抽奖物品
//			model.addEventListener(CustomToClientEvent.SERVER_CUSTMMSG_REMOVE_ITEM,onRemove);
			//发送抽奖物品
			model.addEventListener(CustomToClientEvent.SERVER_CUSTMMSG_SEND_PRIZE_ITEM,onSetPrize);
			//通知客户端抽奖
			model.addEventListener(CustomToClientEvent.SERVER_CUSTMMSG_SCENE_PRIZE,onStarPrize);
			//计时器
//			_timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTime);
		}
		
		/**
		 * 到30秒，关闭界面
		 * @param evt
		 * 
		 */
		private function onTime(evt:TimerEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLOSE_GRADE_VIEEW,2);
			hide();
		}
		
		/**
		 * 绑定银两和仙玉 
		 * 
		 */
		private function initBind():void
		{
			bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",setSilver);
			bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Gold",setGold);
		}
		
		/**
		 * 点击卡牌 
		 * @param evt
		 * 
		 */
		private function onClick(evt:MouseEvent,pos:int):void
		{
			//屏蔽
			if(_isRemove)
			{
				if(!(cardVG.getItemByIndex(pos).getItem() as CardItem).isRemove)
				{
					//最后一张不能屏蔽
					if(_cardNum == 1)
					{
						//提示文字#define STR_SCENEGRADE_CAN_NO_REMONVE					"2385"		//屏蔽次数达到最大，不能屏蔽
						model.dispatchEvent(new ChatEvent(ChatEvent.CLIENT_ALL_SHOW_MSGINFO,StaticIni.getSwfLangStr("2385")));
						return;
					}
//					//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REMOVE_PRIZE,1,pos);
					(cardVG.getItemByIndex(pos).getItem() as CardItem).isRemove = true;
					_cardNum--;
				}
				else
				{
//					//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REMOVE_PRIZE,3,pos);
					(cardVG.getItemByIndex(pos).getItem() as CardItem).isRemove = false;
					_cardNum++;
				}
				getCost();
			}
			//抽奖
			else
			{
				if((cardVG.getItemByIndex(pos).getItem() as CardItem).isMask && !_hasClick)
				{
					//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REMOVE_PRIZE,2,pos);
					_clickPos = pos;
					_hasClick = true;
				}
			}
		}
		
		/**
		 * 获取屏蔽消耗 
		 * 
		 */
		private function getCost():void
		{
			//ClearanceAwardRule.ini屏蔽消耗
			var rule	:int	=	28020000 + (7 - _cardNum);
			var type	:int	=	int(StaticIni.getIniVar(rule,"Type"));
			var value	:int	=	int(StaticIni.getIniVar(rule,"Prize"));
			if(type == MoneyDefine.CAPITAL_TYPE_SILVER)
			{
				_needSilver = value;
				_needGold = 0;
			}
			else
			{
				_needSilver = 0;
				_needGold = value;
			}
			goldLab.htmlStrByBorder = _needGold + StaticIni.getSwfLangStr("1000000066");//仙玉
			silverLab.htmlStrByBorder = _needSilver + StaticIni.getSwfLangStr("1000000067");//银两
			checkMoney();
		}
		
		/**
		 * 显示tips
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onOver(evt:MouseEvent,pos:int):void
		{
			if(!(cardVG.getItemByIndex(pos).getItem() as CardItem).isMask)
			{
				if((cardVG.getItemByIndex(pos).getItem() as CardItem).isItem)
				{
					TipsManage.ShowItemTips((cardVG.getItemByIndex(pos).getItem() as CardItem).configID);
				}
				else
				{
					var type	:int = (cardVG.getItemByIndex(pos).getItem() as CardItem).moneyType;
					switch(type)
					{
						case CardItem.SILVER:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000538",[(cardVG.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
						case CardItem.SPIRIT:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000540",[(cardVG.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
						case CardItem.ENERGY:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000541",[(cardVG.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
					}
				}
			}
		}
		
		private function onOut(evt:MouseEvent,pos:int):void
		{
			ToolTips.hide();
		}
		/**
		 * 关闭 
		 * @param evt
		 * 
		 */
		private function onClose(evt:MouseEvent):void
		{
			//抽奖
			if((evt.currentTarget as Button).label == StaticIni.getSwfLangStr("1000000410"))
			{
				if(_isRemove)
				{
					var posArr	:Array	=	[];
					for(var i:int=0;i<8;i++)
					{
						if(!(cardVG.getItemByIndex(i).getItem() as CardItem).isRemove)
						{
							posArr.push(i);
						}
					}
					//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GRADE_AREA_LUCK_DRAW,posArr);
				}
			}
			//关闭
			else
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLOSE_GRADE_VIEEW,1);
				hide();
			}
		}
		
		/**
		 * 屏蔽掉抽奖物品
		 * @param evt
		 * 
		 */
		private function onRemove(evt:CustomToClientEvent):void
		{
			var type	:int = evt.pList.shift();
			var pos		:int = evt.pList.shift();
			var tmpItem	:CardItem = cardVG.getItemByIndex(pos).getItem() as CardItem;
			//1 屏蔽物品 2 取消屏蔽
			if(type == 1)
			{
				tmpItem.isRemove = true;
				_cardNum--;
			}
			else
			{
				tmpItem.isRemove = false;
				_cardNum++;
			}
			_needGold = evt.pList.shift();
			_needSilver = evt.pList.shift();
			goldLab.htmlStrByBorder = _needGold + StaticIni.getSwfLangStr("1000000066");//仙玉
			silverLab.htmlStrByBorder = _needSilver + StaticIni.getSwfLangStr("1000000067");//银两
			checkMoney();
		}
		
		/**
		 * 检查银两是否足够 
		 * 
		 */
		private function checkMoney():void
		{
			changeColor(goldLab,_needGold,_hasGold);
			changeColor(silverLab,_needSilver,_hasSilver);
		}
		
		/**
		 * 改变颜色 
		 * 
		 */
		private function changeColor(lab:Label,needNum:int,hasNum:int):void
		{
			if(needNum > hasNum)
			{
				lab.color = "FF0000";
			}
			else
			{
				lab.color = "FFFFFF";
			}
		}
		
		/**
		 * 发送抽奖物品
		 * @param evt
		 * 
		 */
		private function onSetPrize(evt:CustomToClientEvent):void
		{
			setCard(evt.pList.shift(),evt.pList.shift(),evt.pList.shift(),_clickPos);
			closeBtn.label = StaticIni.getSwfLangStr("1000000409");
			clearData();
		}
		
		/**
		 * 收到消息把牌翻过去 
		 * @param evt
		 * 
		 */
		private function onStarPrize(evt:CustomToClientEvent):void
		{
			_isRemove = false;
			moneySp.visible = false;
			maskCard();
		}
		
		/**
		 * 显示评定抽奖 
		 * @param evt
		 * 
		 */
		public function showResult(evt:CustomToClientEvent):void
		{
			show();
			/*_timer.reset();
			_timer.start();*/
			//添加星星
			setStar(evt.pList.shift());
			//场景id
			_curScene	= evt.pList.shift();
			//处理抽奖
			var pos	:int;
			while(evt.pList.length>2)
			{
				setCard(evt.pList.shift(),evt.pList.shift(),evt.pList.shift(),pos);
				pos++;
			}
			//第一次屏蔽免费
			silverLab.htmlStrByBorder = 0 + StaticIni.getSwfLangStr("1000000067");//银两
			goldLab.htmlStrByBorder = 0 + StaticIni.getSwfLangStr("1000000066");//仙玉
		}
		
		/**
		 *  显示星星 
		 * 
		 */
		private function setStar(star:int):void
		{
			for(var i:int=0;i<star;i++)
			{
				var mc	:Clip = new Clip(true,null,"jzLogo");
				mc.source = ResConfig.pngResPath + "tips_star2.png";
				mc.x = 100 + 50 * i;
				mc.y = 30;
				starSp.addChild(mc);
			}
		}
		
		/**
		 * 设置卡牌 
		 * 
		 */
		private function setCard(type:int,itemID:int,anoumt:int,pos:int):void
		{
			var tmpItem	:CardItem;
			if(null == cardVG.getItemByIndex(pos).getItem())
			{
				tmpItem = new CardItem();
				cardVG.getItemByIndex(pos).addItem(tmpItem);
			}
			else
			{
				tmpItem = cardVG.getItemByIndex(pos).getItem() as CardItem;
			}
			tmpItem.setData(type,itemID,anoumt);
		}
		
		/**
		 * 把牌翻过去 
		 * 
		 */
		private function maskCard():void
		{
			clearCard();
			var tmpItem	:CardItem;
			for(var i:int=0;i<_cardNum;i++)
			{
				tmpItem = cardVG.getItemByIndex(i).getItem() as CardItem;
				tmpItem.maskCard();
			}
		}
		
		/**
		 * 现有的银两 
		 * @param key
		 * @param val
		 * 
		 */
		private function setSilver(key:String,val:int):void
		{
			_hasSilver = val;
			hasSilver.htmlStrByBorder = MoneyTools.setMoney(_hasSilver) + StaticIni.getSwfLangStr("1000000067");
			checkMoney();
		}
		
		/**
		 * 现有的仙玉 
		 * @param key
		 * @param val
		 * 
		 */
		private function setGold(key:String,val:int):void
		{
			_hasGold = val;
			hasGold.htmlStrByBorder = MoneyTools.setMoney(_hasGold) + StaticIni.getSwfLangStr("1000000066");
			checkMoney();
		}
		
		/**
		 * 清除卡牌内容
		 * 
		 */
		private function clearCard():void
		{
			cardVG.getItemForEach(function (tmpItem:BaseItem):void
			{
				(tmpItem.getItem() as CardItem).clearAll();
			});
		}
		
		/**
		 * 清除星星 
		 * 
		 */
		private function clearStar():void
		{
			while(starSp.numChildren>0)
			{
				starSp.removeChildAt(0);
			}
		}
		
		/**
		 * 清除数据 
		 * 
		 */
		private function clearData():void
		{
			//金钱处理
			_needGold = 0;
			_needSilver = 0;
			checkMoney();
		}
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			//卡牌重置
			_cardNum = 8;
			_isRemove = true;
			_hasClick = false;
			closeBtn.label = StaticIni.getSwfLangStr("1000000410");
			moneySp.visible = true;
			super.show(space,onshow);
		}
		
		override public function hide(e:*=null):void
		{
			super.hide(e);
			clearStar();
			clearCard();
//			_timer.stop();
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			//调整遮罩位置
			maskMc.width = GameStage.stageWidth + 10;
			maskMc.height = GameStage.stageHeight + 10;
			maskMc.x	=	initX - GameStage.mainLayer.x - 630;
			maskMc.y 	= 	initY - GameStage.mainLayer.y - 355;
		}
	}
}